<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/8
 * Time: 8:46
 */

namespace app\admin\controller;

use app\admin\model\Banner;
use app\admin\model\BannerItem;
use think\Request;


class BanneritemController extends BaseController
{
    //显示当前广告位下的的广告图片
    public function lst()
    {

        $params = $this->request->param();
        $map = [];
        if(isset($params['banner_id'])){
            $map['banner_id'] = $params['banner_id'];
        }
        else{
            $params['banner_id']  = '';
        }
        $list = BannerItem::with('banner')->where($map)->paginate(7);
        $page = $list->render();
        $this->assign([
            'banner_id' => $params['banner_id'],
            "list" => $list,
            'page' => $page,
        ]);
        return $this->fetch();
    }


    //为当前对应的广告位下添加图片
    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $banner_id = $request->param('banner_id');
                $this->assign([
                    'banner_id' => $banner_id,
                    'banner' => Banner::all(),
                ]);
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $controller = strtolower($request->controller());
                if(request()->file('pic')){
                    $picInfo = uploadOne($controller,'thumb','pic',200,150);
                    $params['pic'] = $picInfo['pic'];
                    $params['sm_pic'] = $picInfo['thumb_pic'];
                }
                $model = new BannerItem();
                if($model->allowField(true)->save($params)){
                    $this->success('添加广告位内容信息成功!',url('admin/banneritem/lst',['banner_id'=>$model->banner_id]),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    //广告位图片修改
    public function edi()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $id = $request->param('id');
                $this->assign([
                    "banner" =>  Banner::all(),
                    'list'   =>  BannerItem::find($id)
                ]);
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new BannerItem();
                if(request()->file('pic')){
                    $oldPic = $model->where('id','eq',$params['id'])->find();
                    if(is_file($oldPic['pic'])){
                        unlink($oldPic['pic']);
                    }
                    if(is_file($oldPic['sm_pic'])){
                        unlink($oldPic['sm_pic']);
                    }
                    $controller = strtolower($request->controller());
                    $picInfo = uploadOne($controller,'thumb','pic',200,150);
                    $params['pic'] = $picInfo['pic'];
                    $params['sm_pic'] = $picInfo['thumb_pic'];
                }

                if($model->allowField(true)->isUpdate(true)->save($params)){
                    $this->success('修改广告位内容信息成功!',url('admin/banneritem/lst',['banner_id'=>$model->banner_id]),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function del()
    {
        $id = $this->request->param("id");
        $model = BannerItem::find($id);
        if(is_file($model->pic)){
            unlink($model->pic);
        }
        if(is_file($model->sm_pic)){
            unlink($model->sm_pic);
        }
        if(BannerItem::destroy($id)){
            return '亲,你已删除成功^o^';
        }
        return '删除失败,what...';
    }
}